﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Security.Cryptography;
using System.Configuration;

namespace DusonFramework.Core.Security
{
    public class CryptographyManager
    {
        #region Private Fields

        private CryptographyType _cryptType;
        private SymmetricType _symmetricType;
        private HashAlgorithmType _hashType;

        #endregion

        #region Public Properties

        public CryptographyType CryptType 
        {
            get { return _cryptType; }
            set { _cryptType = value; }
        }

        public SymmetricType SymmetricType 
        {
            get { return _symmetricType; }
            set { _symmetricType = value; }
        }

        public HashAlgorithmType HashType 
        {
            get { return _hashType; }
            set { _hashType = value; }
        }

        #endregion

        #region Constructor

        public CryptographyManager() { }

        public CryptographyManager(CryptographyType cryptType) 
        {
            _cryptType = cryptType;
        }
        public CryptographyManager(SymmetricType symmetricType) 
        {
            _symmetricType = symmetricType;
        }
        public CryptographyManager(HashAlgorithmType hashType) 
        {
            _hashType = hashType;
        }

        #endregion

        #region Public Method

        public string Encode(string plainText)
        {
            return LoadProvider().Encrypting(plainText);
        }

        public string Decode(string cypherText)
        {
            return LoadProvider().Decrypting(cypherText);
        }

        #endregion

        #region Private method

        private ICryptographyProvider LoadProvider()
        {
            ICryptographyProvider result = CryptoFactory.NonCrptoCreate();

            switch (_cryptType)
            {
                case CryptographyType.Encrypted:
                    result = CryptoFactory.SymmetricCreate(_symmetricType);
                    break;
                case CryptographyType.Hashed:
                    result = CryptoFactory.HashCreate(_hashType);
                    break;
                default:
                    result = CryptoFactory.NonCrptoCreate();
                    break;
            }

            return result;
        }

        #endregion
    }
}
